package com.entrance;

import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.sensor.Fragment.ShenYaJiFragment;
import com.entrance.backgroundaction.BackgroundService;
import com.landmesure.bluetoothchat.R;
import com.landmesure.bluetoothchat.view.BluetoothFragment;
import com.sensor.Fragment.WaterMonitorFragment;
import com.sensor.ui.WaterMonitorActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    public FragmentUpdate mFragmentUpdate;
    public Fragment[][] mFragments;

    private boolean mIsBind;
    private BackgroundService mService;
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ((BackgroundService.RSBinder) service).getService();
            mIsBind = true;
            mFragmentUpdate.updateFragment(R.id.main_layout, mFragments[0][0]);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mIsBind = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentUpdate = new FragmentUpdate(this);
        initFragment();
        setContentView(R.layout.activity_main);

        //绑定后台服务
        Intent intent = new Intent(this, BackgroundService.class);
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

        //初始化layout界面
        initLayout();
    }

    private void initFragment() {
        mFragments = new Fragment[][] {
                new Fragment[] {new BluetoothFragment()},
                new Fragment[]{new ShenYaJiFragment(),new WaterMonitorFragment()},
        };
    }

    /**
     * DrawerLayout界面
     */
    private void initLayout() {
        final DrawerLayout drawerLayout = findViewById(R.id.main_drawer_layout);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                drawerLayout.getChildAt(0)
                        .setTranslationX(drawerView.getMeasuredWidth() * slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) { }

            @Override
            public void onDrawerClosed(View drawerView) { }

            @Override
            public void onDrawerStateChanged(int newState) { }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mIsBind) {
            unbindService(mServiceConnection);
            mIsBind = false;
        }
    }

    public BackgroundService getService() {
        return mService;
    }
}
